module.exports = {
  /**
  * 剪切字符串
  * @ str 原字符串
  * @ 最大长度   
  */
  MsgCutWord: function (str, num) {
    if (str === null) { str = ""; }
    var result = str;
    var wordLength = str.length;
    if (wordLength > num) {
      result = str.substr(0, num);
      result += "...";
    }
    return (result);
  },

  /**
  *  修改日期格式年月日
  *  @ jsondate json日期
  *  @ splitchar 分隔符
  */
  ChangeDateFormat: function (jsondate, splitchar) {
    if (!jsondate) { return ""; }
    jsondate = jsondate.replace("/Date(", "").replace(")/", "");
    if (jsondate.indexOf("+") > 0) {
      jsondate = jsondate.substring(0, jsondate.indexOf("+"));
    }
    else if (jsondate.indexOf("-") > 0) {
      jsondate = jsondate.substring(0, jsondate.indexOf("-"));
    }
    var date = new Date(parseInt(jsondate, 10));
    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
    var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    return date.getFullYear() + splitchar + month + splitchar + currentDate;
  },

  /**
  *  修改日期格式年月日
  *  @ jsondate json日期
  *  @ splitchar 分隔符
  */
  ChangeDateFormat_hhmmss: function (jsondate, splitchar) {
    if (!jsondate) { return ""; }
    jsondate = jsondate.replace("/Date(", "").replace(")/", "");
    if (jsondate.indexOf("+") > 0) {
      jsondate = jsondate.substring(0, jsondate.indexOf("+"));
    }
    else if (jsondate.indexOf("-") > 0) {
      jsondate = jsondate.substring(0, jsondate.indexOf("-"));
    }
    var date = new Date(parseInt(jsondate, 10));
    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
    var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    var hh = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
    var mm = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    return date.getFullYear() + splitchar + month + splitchar + currentDate + " " + hh + ":" + mm;
  },

  /**
  *  获得当天对应的天数
  *  @ jsondate json日期
  *  @ splitchar 分隔符
  */
  GetDateDay: function (jsondate) {
    if (!jsondate) { return ""; }
    jsondate = jsondate.replace("/Date(", "").replace(")/", "");
    if (jsondate.indexOf("+") > 0) {
      jsondate = jsondate.substring(0, jsondate.indexOf("+"));
    }
    else if (jsondate.indexOf("-") > 0) {
      jsondate = jsondate.substring(0, jsondate.indexOf("-"));
    }
    var date = new Date(parseInt(jsondate, 10));
    return date.getDate();
  },

  /**
  * 设置Cookie 【js版】
  * @ name 名称
  * @ value 内容
  * @ time 保存时间
  * @ path [可选] 路径
  * 删除时time传 -1
  */
  MsgSetCookie: function (name, value, time, path) {
    path = path || "";
    var d = new Date();
    d.setTime(d.getTime() + (time * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    path = "path=" + path;
    document.cookie = name + "=" + value + "; " + expires + "; " + path;
  },

  /**
  * 读取Cookie 【js版】
  * @ name 名称
  */
  MsgGetCookie: function (name) {
    name = (name + "=");
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0) === ' ') c = c.substring(1);
      if (c.indexOf(name) !== -1) return c.substring(name.length, c.length);
    }
    return "";
  },


  /**
  * 打印对象属性和方法
  * @ obj 对象
  */
  ShowObjectAttr: function (obj) {
    for (var name in obj) {
      console.log(name + ": " + obj[name]);
    }
  },

  /**
  * 获取纯文字
  * @ html 富文本/html标签
  * @ length 长度 可选[默认全部]
  */
  MsgGetPureText: function (html, length) {
    var text = html.replace(/<[^>]+>/g, "");
    length = isNaN(length) ? text.length : length;
    return text.substring(0, length) + "...";
  },

  /**
  * 翻译状态
  * @ state
  */
  TranslateState: function (state) {
    switch (state) {
      case "0": return "待受理";
      case "1": return "待受理";
      case "2": return "已受理";
      case "3": return "不受理";
      case "4": return "已取消";
      case "5": return "被取消";
      default: return "未定义";
    }
  },

  /**
  * 邮件判断
  * @ email
  */
  ValidateEmail: function (email) {
    var myreg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
    return myreg.test(email);
  },

  /**
  * 电话判断
  * @ phone
  */
  ValidatePhone: function (phone) {
    if (!phone) { return false; }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    return myreg.test(phone);
  },


  /**
  * 监听图片错误时替换图片
  * [src] 替换路径
  */
  ListenImgsError: function (src) {
    if (!src) { src = "http://localhost:13301/img/default_404.jpg"; }
    var imgs = document.getElementsByTagName("img");
    for (var i = 0; i < imgs.length; i++) {
      var img = imgs[i];
      img.addEventListener("error", function (event) {
        if (this.src === src) {
          this.onerror = null;
          return;
        }
        this.src = src;
        this.alt = "404"
      }, false);
    }
  },

  /**
  * 用户sessionid(外部不可见)
  */
  _UserSessionId: "",

  /*
  * 设置sessionId
  *   默认设置到JavascriptCommon中
  *   其次设置到localStorage中
  *   再次设置到cookie中
  */
  SetUserSessionId: function (sessionId) {
    this._UserSessionId = sessionId;
    if (typeof window.localStorage === "object") {
      localStorage.setItem("UserSessionId", sessionId);
    } else {
      this.MsgSetCookie("UserSessionId", sessionId, (1 / 24 / 60) * 30);
    }
  },

  /**
  * 读取sessionId
  *   默认读取JavascriptCommon
  *   其次读取localStorage
  *   再次读取cookie
  */
  GetUserSessionId: function () {
    if (!this._UserSessionId) {
      if (typeof window.localStorage === "object") {
        return localStorage.getItem("UserSessionId");
      } else {
        return this.MsgGetCookie("UserSessionId");
      }
    } else {
      return this._UserSessionId;
    }
  },

  /**
  * 清空sessionId
  *   全清
  */
  RemoveUserSessionId: function () {
    this._UserSessionId = "";
    if (typeof window.localStorage === "object") {
      sessionStorage.removeItem("UserSessionId");
    }
    this.MsgSetCookie("UserSessionId", "", -1);
  },

  /**
   * guid
   */
  Guid: function () {
    var guid = "";
    for (var i = 1; i <= 32; i++) {
      var n = Math.floor(Math.random() * 16.0).toString(16);
      guid += n;
      if ((i === 8) || (i === 12) || (i === 16) || (i === 20))
        guid += "_";
    }
    return guid;
  },


  /**
   * ajax加载提示
   * @flag 是否加载
   */
  AjaxLoading: function (flag) {
    if (flag) {
      document.getElementById("global_ajax_loading").style.display = "block";
    } else {
      document.getElementById("global_ajax_loading").style.display = "None";
    }
  },

  /**
   * 判断是否是微信内置浏览器
   */
  IsWeixinBrowser: function () {
    return window.navigator.userAgent.toLowerCase().indexOf("micromessenger") > -1;
  }
};
